package ai.yue.open.service.cd.service;

import ai.yue.library.base.convert.Convert;
import ai.yue.library.base.view.R;
import ai.yue.library.base.view.Result;
import ai.yue.library.web.util.ServletUtils;
import ai.yue.open.service.cd.entity.ConfRancher;
import ai.yue.open.service.cd.ipo.ConfRancherIPO;
import ai.yue.open.service.cd.mapper.ConfRancherMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 配置-bearer_token
 *
 * @author yl-yue
 * @since  2023/2/7
 */
@Service
public class ConfRancherService extends ServiceImpl<ConfRancherMapper, ConfRancher> {

    List<ConfRancher> confRancherList;

    public Result<?> insert(ConfRancherIPO confRancherIPO) {
        boolean save = save(Convert.toJavaBean(confRancherIPO, ConfRancher.class));
        confRancherList = null;
        return R.success(save);
    }

    public Result<?> deleteById(Long id) {
        boolean removeById = removeById(id);
        confRancherList = null;
        return R.success(removeById);
    }

    public Result<?> deleteByIds(List<Long> ids) {
        boolean removeByIds = removeByIds(ids);
        confRancherList = null;
        return R.success(removeByIds);
    }

    public Result<?> updateById(ConfRancherIPO confRancherIPO) {
        boolean updateById = updateById(Convert.toJavaBean(confRancherIPO, ConfRancher.class));
        confRancherList = null;
        return R.success(updateById);
    }

    public Result<?> page() {
        PageHelper.startPage(ServletUtils.getRequest());
        List<ConfRancher> list = list();
        return R.success(PageInfo.of(list));
    }

    public List<ConfRancher> getConfRancherList() {
        if (confRancherList == null) {
            confRancherList = list();
        }

        return confRancherList;
    }

    public Result<?> listNamespace() {
        List<ConfRancher> confRancherList = getConfRancherList();
        List<String> namespaceList = new ArrayList<>();
        confRancherList.forEach(confRancher -> {
            namespaceList.addAll(confRancher.getNamespaceList());
        });
        return R.success(namespaceList);
    }

}
